﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class G001_NewUserTest {
		private static IWebDriver driver;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			driver = TestHelper.GetDriver();
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void TestRegister() {
			driver.VisitURL("forum/account/register");

			driver.InsertText("Username", "Mr. New User");

			driver.InsertText("EmailAddress", "newuser@mvcforum.org");

			driver.InsertText("Password", "123456");

			driver.InsertText("ConfirmPassword", "123456");

			IWebElement button = driver.FindElement(By.Name("post"));
			//IWebElement button = driver.FindButton("Register");
			Assert.IsNotNull(button);
			button.Click();

			Assert.AreEqual(String.Format("{0}forum/account/success", TestConfiguration.SiteBaseURL), driver.Url.ToLower());
		}

		[Test]
		public void TestRegisterDoubleUsername() {
			driver.VisitURL("forum/account/register");

			driver.InsertText("Username", Constants.Users.Regular.Username);

			driver.InsertText("EmailAddress", "reegular@mvcforum.org");

			driver.InsertText("Password", "blabla");

			driver.InsertText("ConfirmPassword", "blabla");

			IWebElement button = driver.FindElement(By.Name("post"));
			//IWebElement button = driver.FindButton("Register");
			Assert.IsNotNull(button);
			button.Click();

			Assert.AreEqual(String.Format("{0}forum/account/register", TestConfiguration.SiteBaseURL), driver.Url.ToLower());

			Assert.AreEqual(true, driver.PageSource.Contains("is already taken") || driver.PageSource.Contains("User name already exists."));

			driver.InsertText("Username", "MrTest2");

			driver.InsertText("EmailAddress", Constants.Users.Regular.Email);

			driver.InsertText("Password", "blabla");

			driver.InsertText("ConfirmPassword", "blabla");

			button = driver.FindElement(By.Name("post"));
			//button = driver.FindButton("Register");
			button.Click();

			Assert.AreEqual(String.Format("{0}forum/account/register", TestConfiguration.SiteBaseURL), driver.Url.ToLower(), "Url not as expected");

			Assert.AreEqual(true, driver.PageSource.Contains("A user name for that e-mail address") || driver.PageSource.Contains(" is already taken"), "Error message not found on page");
		}

		[Test]
		public void TestRegisterLogOn() {
			Assert.AreEqual(true, driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password), "Could not log on");
		}
	}
}